const fs = require("fs");
const path = require("path");

let optionFile = "ffmpeg/option.txt";
let detailFile = "ffmpeg/detail.txt";

let ffprobeOptionFile = "ffprobe/option.txt";
let ffprobeDetailFile = "ffprobe/detail.txt";

let ffplayOptionFile = "ffplay/option.txt";
let ffplayDetailFile = "ffplay/detail.txt";

function parseOption(optionFile) {
    fs.readFile(optionFile, "utf8", (err, data) => {
        if (err) {
            l(optionFile + "read err", err);
        }
        // l(data);
        let arr = data.split(/[\r\n]/)
            .filter(d => d)
            .map(d => d.trim());
        let block = [];
        let blockIndex = 0;
        arr.forEach(line => {
            if (line[line.length - 1] == ":") {  //block
                let currentO = {
                    id: line,
                    flags: []
                }
                block.push(currentO);
                blockIndex = blockIndex + 1;
            } else if (line[0] == "-") {
                let tempFlag = line
                    .split("  ")
                    .filter(d => d)
                    .map(d => d.trim())
                    .concat(["备注"]);
                block[blockIndex - 1].flags.push(tempFlag);
            }
        })
        // ls(block,0);
        fs.writeFile(
            path.join(__dirname, "/" + optionFile.replace(".txt", ".json")),
            JSON.stringify(block),
            (err, data) => {
                if (err) {
                    l("write err", err)
                } else {
                    l("ok");
                }
            }
        )
    })
}

function parseDetail(detailFile) {
    fs.readFile(detailFile, "utf8", (err, data) => {
        if (err) {
            l(optionFile + "read err", err);
        }
        // l(data);
        let arr = data
            .split(/[\r\n]/)
            .filter(d => d)
            .map(d => d.trim());
        // l(arr);
        let block = [];
        /**
         * {
         *      id:"",
         *      flags:[
         *          "prefix":"",
         *          "type":"",
         *          "enums":[
         *              "value":"",
         *              "detail":""
         *           ]
         *      ]
         * }
         * {
         *      "id":"",
         *      "f":[
         *          "p":"",
         *          "t":"",
         *          "d":"",
         *          "z":"",
         *          "e":["v":"","d":""]
         *      ]
         * }
         */
        let currentO = {};
        let flagIndex = 0;

        arr.forEach(line => {
            // l(line.includes,line);
            if (line[line.length - 1] == ":") {  //block
                currentO = {
                    id: line,
                    flags: []
                }
                block.push(currentO);
                flagIndex = 0;
            } else if (line[0] == "-"
                || (line.length > 0 && line.includes("<") && line.includes(">"))) {
                let tempFlag = line
                    .split(" ")
                    .filter(d => d)
                    .map(d => d.trim());
                let [p, t, ...d] = tempFlag;
                // l("ccc", p, t, d);
                currentO.flags.push({
                    p: p,
                    t: t,
                    d: d.join(" "),
                    z: "备注",
                    e: []
                });
                // ls(currentO,2);
                flagIndex = flagIndex + 1;
            } else {
                // currentO.flags[flagIndex]
                // l("dddd", currentO.flags[flagIndex -1], line);
                let tempEnum = line
                    .split(" ")
                    .filter(d => d)
                    .map(d => d.trim());
                let [v, ...d] = tempEnum;

                try {
                    currentO.flags[flagIndex - 1]["e"].push({
                        v: v,
                        d: d.join(" "),
                        z: "备注"
                    });
                } catch (error) {
                    l(line, currentO);
                }


            }
        })

        fs.writeFile(
            path.join(__dirname, "/" + detailFile.replace(".txt", ".json")),
            JSON.stringify(block),
            (err, data) => {
                if (err) {
                    l("write err", err)
                } else {
                    l("ok");
                }
            }
        )
    })
}

parseOption(optionFile);
parseDetail(detailFile);


parseOption(ffprobeOptionFile);
parseDetail(ffprobeDetailFile);


parseOption(ffplayOptionFile);
parseDetail(ffplayDetailFile);

function l(...msg) {
    console.log(msg);
}

function ls(msg, spaceNum = 0) {
    console.log(JSON.stringify(msg, null, spaceNum));
}

